let app = getApp();
Page({
  data: {
    url: '',              
    courses: [], 
    consultantId: null, 
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log('接收的参数：', options);
    const consultantId = parseInt(options.consultantId);
    if (isNaN(consultantId)) {
      wx.showToast({
        title: '参数错误，无法加载课程',
        icon: 'none',
        duration: 2000
      });
      setTimeout(() => {
        wx.navigateBack();
      }, 2000);
      return;
    }

    this.setData({
      consultantId: consultantId,
      url: app.globalData.url 
    });

    this.fetchData(); 
  },

  fetchData() {
    const consultantId = this.data.consultantId;
    wx.request({
      url: this.data.url + 'consultant/' + consultantId + '/course',
      method: 'GET', 
      success: (res) => {
        console.log('课程接口返回:', res.data);
        if (res.data && res.data.code === 200) {
          this.setData({
            courses: res.data.data || [] 
          });
        } else {
          wx.showToast({
            title: res.data?.msg || '课程数据加载失败',
            icon: 'none'
          });
        }
      },
      fail: (err) => {
        console.error('课程请求失败：', err);
        wx.showToast({
          title: '网络错误，无法加载课程',
          icon: 'none'
        });
      }
    });
  },
})